package org.sdfc.query

import com.documentum.fc.client.DfQuery
import com.documentum.fc.client.IDfSession
import com.documentum.fc.common.DfException

import org.sdfc.model.dfc.collection.RichCollection

/** Query variant where the queryType is fixed by the class itself.
 */
abstract class TypedQuery (
  /** DQL of the query. */
  val dql : String
) {
  
  /** queryType to be used by this. */
  def queryType : Int
  
  
  /** Executes this and returns the results in a RichCollection.
   */
  @throws(classOf[DfException])
  def execute(session : IDfSession) : RichCollection = {
    val query = new DfQuery
    query.setDQL(dql)
    new RichCollection( query.execute(session, queryType) )
  }
  
  
  /** Prepares this for execution in a PreparedQuery which provides useful extraction methods.
   */
  def prepare(session : IDfSession) : PreparedQuery = new PreparedQuery(this, session)
  
  
  /** Prepares this as a sequence of at most one CollectionRow usable with sequence comprehension syntax.
   */
  def firstRow(session : IDfSession) : PreparedFirstRow = new PreparedFirstRow(this, session)
  
  /** Prepares this as a sequence of CollectionRow usable with sequence comprehension syntax.
   */
  def rows(session : IDfSession) : PreparedRows = new PreparedRows(this, session)
  
}
